{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4103c733",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install --pre -U langchain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "253469c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install -U langchain-openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcc417a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install -U langchain-deepseek"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6b3fc14",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32feb540",
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a agent\n",
    "from langchain.agents import create_agent\n",
    "\n",
    "def get_weather(city: str) -> str:\n",
    "    \"\"\"Get weather for a given city.\"\"\"\n",
    "    return f\"It's always sunny in {city}!\"\n",
    "\n",
    "agent = create_agent(\n",
    "    model=\"deepseek:deepseek-chat\",\n",
    "    tools=[get_weather],\n",
    "    prompt=\"You are a helpful assistant\",\n",
    ")\n",
    "\n",
    "# Run the agent\n",
    "agent.invoke(\n",
    "    {\"messages\": [{\"role\": \"user\", \"content\": \"what is the weather in sf\"}]}\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25a3a849",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_prompt = \"\"\"You are an expert weather forecaster, who speaks in puns.\n",
    "\n",
    "You have access to two tools:\n",
    "\n",
    "- get_weather_for_location: use this to get the weather for a specific location\n",
    "- get_user_location: use this to get the user's location\n",
    "\n",
    "If a user asks you for the weather, make sure you know the location. If you can tell from the question that they mean whereever they are, use the get_user_location tool to find their location.\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bce82041",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.tools import tool\n",
    "\n",
    "def get_weather_for_location(city: str) -> str:  # (1)!\n",
    "    \"\"\"Get weather for a given city.\"\"\"\n",
    "    return f\"It's always sunny in {city}!\"\n",
    "\n",
    "from langchain_core.runnables import RunnableConfig\n",
    "\n",
    "USER_LOCATION = {\n",
    "    \"1\": \"Florida\",\n",
    "    \"2\": \"SF\"\n",
    "}\n",
    "\n",
    "@tool\n",
    "def get_user_location(config: RunnableConfig) -> str:\n",
    "    \"\"\"Retrieve user information based on user ID.\"\"\"\n",
    "    user_id = config[\"context\"].get(\"user_id\")\n",
    "    return USER_LOCATION[user_id]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3aa70fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import init_chat_model\n",
    "\n",
    "llm = init_chat_model(\n",
    "    \"deepseek:deepseek-chat\",\n",
    "    temperature=0\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1da151f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataclasses import dataclass\n",
    "\n",
    "@dataclass\n",
    "class WeatherResponse:\n",
    "    conditions: str\n",
    "    punny_response: str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8aab3a7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langgraph.checkpoint.memory import InMemorySaver\n",
    "\n",
    "checkpointer = InMemorySaver()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b26296fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import create_agent\n",
    "\n",
    "agent = create_agent(\n",
    "    model=llm,\n",
    "    prompt=system_prompt,\n",
    "    tools=[get_user_location, get_weather_for_location],\n",
    "    response_format=WeatherResponse,\n",
    "    checkpointer=checkpointer\n",
    ")\n",
    "\n",
    "config = {\"configurable\": {\"thread_id\": \"1\"}}\n",
    "context = {\"user_id\": \"1\"}\n",
    "response = agent.invoke(\n",
    "    {\"messages\": [{\"role\": \"user\", \"content\": \"what is the weather outside?\"}]},\n",
    "    config=config,\n",
    "    context=context\n",
    ")\n",
    "\n",
    "response['structured_response']\n",
    "\n",
    "response = agent.invoke(\n",
    "    {\"messages\": [{\"role\": \"user\", \"content\": \"thank you!\"}]},\n",
    "    config=config,\n",
    "    context=context\n",
    ")\n",
    "\n",
    "response['structured_response']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e63c9200",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import create_agent\n",
    "from langgraph.checkpoint.memory import InMemorySaver\n",
    "\n",
    "agent = create_agent(\n",
    "    \"deepseek:deepseek-chat\",\n",
    "    [],\n",
    "    checkpointer=InMemorySaver(),\n",
    ")\n",
    "\n",
    "agent.invoke(\n",
    "    {\"messages\": [{\"role\": \"user\", \"content\": \"Hi! My name is Bob.\"}]},\n",
    "    {\"configurable\": {\"thread_id\": \"1\"}},\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9101b4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent.invoke(\n",
    "    {\"messages\": [{\"role\": \"user\", \"content\": \"what is my name?\"}]},\n",
    "    {\"configurable\": {\"thread_id\": \"1\"}},\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain_learn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
